package main

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"net/http"
	"regexp"
	"strings"
)

func main() {
	spider()
}

func spider() {
	// 1、获取页面源代码
	client := http.Client{}
	req, err := http.NewRequest("GET", "https://movie.douban.com/top250", nil)
	if err != nil {
		fmt.Print(err)
	}
	// 1.1、添加请求头，反爬虫
	req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0")
	resp, err := client.Do(req)
	if err != nil {
		print(err)
	}
	// 2、解析网页
	//fmt.Print(resp.Body)
	docDetail, err := goquery.NewDocumentFromReader(resp.Body)
	if err != nil {
		print(err)
	}
	docDetail.Find("#content > div > div.article > ol > li"). // 通过CSS找到目标根节点
									Each(func(i int, selection *goquery.Selection) {
			title := selection.Find("div > div.info > div.hd > a > span:nth-child(1)").Text()                     // 获取标签文本内容
			link, _ := selection.Find("div > div.info > div.hd > a").Attr("href")                                 // 获取属性值
			result := strings.TrimSpace(selection.Find("div > div.info > div.bd > div > span.rating_num").Text()) // 标签名.类值 || 标签名#id值
			personNum := strings.TrimSpace(selection.Find("div > div.info > div.bd > div > span").Text())
			info := selection.Find("div > div.info > div.bd > p:nth-child(1)").Text()
			director, actor, year := infoSpite(info)
			fmt.Print("片名：", title, "\n", strings.TrimSpace(director), " ", strings.TrimSpace(actor), " ", year, "\n 评价人数：", personNum, " 评价分数：", result, "/10", "\n")
			fmt.Println("链接：", link, "\n")
		}) // 根据上述节点继续查询
}

func infoSpite(info string) (director, actor, year string) {
	yearRe, _ := regexp.Compile(`\d+`) // 正则表达式匹配年份
	re := regexp.MustCompile(`(导演:[^主]+)主演`)
	matches := re.FindStringSubmatch(info)
	if len(matches) > 1 {
		director = matches[1] // 提取捕获组内容
	} // 正则表达式匹配年份
	actorRe, _ := regexp.Compile("主演(.*)") // 正则表达式匹配年份
	year = yearRe.FindString(info)
	//director = strings.Split(directorRe.FindString(info), "主")[0]
	actor = actorRe.FindString(info)

	return director, actor, year
}

func write() {

}
